/*
 * Copyright (c) 2012-2013 Open Source Community - <http://www.peerfact.org>
 * Copyright (c) 2011-2012 University of Paderborn - UPB
 * Copyright (c) 2005-2011 KOM - Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.peerfact.impl.service.aggregation.skyeye.metrics;

import org.apache.log4j.Logger;
import org.peerfact.api.service.skyeye.SkyNetNodeInterface;
import org.peerfact.impl.service.aggregation.skyeye.DHTParamterManipulator;
import org.peerfact.impl.service.aggregation.skyeye.SkyNetUtilities;
import org.peerfact.impl.simengine.Simulator;
import org.peerfact.impl.util.logging.SimLogger;


//TODO implement class

/**
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 * This part of the Simulator is not maintained in the current version of
 * PeerfactSim.KOM. There is no intention of the authors to fix this
 * circumstances, since the changes needed are huge compared to overall benefit.
 * 
 * If you want it to work correctly, you are free to make the specific changes
 * and provide it to the community.
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 * 
 * This class shall be used to distribute the information, which is generated by
 * the root of the SkyNet-tree based on the received metrics.
 * <code>MetricsInterpretation</code> can contain the information about the
 * generated system-statistics, the classified state of the whole P2P-system and
 * possible behavioral rules for the participating nodes of SkyNet. The
 * implemented methods of this class are currently used to demonstrate the
 * functioning of the dissemination of information by the root.
 * 
 * @author Dominik Stingl <peerfact@kom.tu-darmstadt.de>
 * @version 1.0, 06.12.2008
 * 
 */
public class MetricsInterpretation {

	private static Logger log = SimLogger
			.getLogger(MetricsInterpretation.class);

	private SkyNetNodeInterface skyNetNode;

	private MetricStorage storage;

	private MetricsEntry actualSystemStatistics;

	private DHTParamterManipulator paraManipulator;

	private long statisticsTimestamp;

	private long manipulatorTimestamp;

	private long receivingTimestamp;

	public MetricsInterpretation(SkyNetNodeInterface skyNetNode,
			MetricStorage storage) {
		this.skyNetNode = skyNetNode;
		this.storage = storage;
		actualSystemStatistics = null;
		paraManipulator = null;
		statisticsTimestamp = 0;
		manipulatorTimestamp = 0;
		receivingTimestamp = 0;
	}

	public void reset() {
		actualSystemStatistics = null;
		paraManipulator = null;
		statisticsTimestamp = 0;
		manipulatorTimestamp = 0;
		receivingTimestamp = 0;
	}

	public MetricStorage getStorage() {
		return storage;
	}

	public void setStorage(MetricStorage storage) {
		this.storage = storage;
	}

	public MetricsEntry getActualSystemStatistics() {
		return actualSystemStatistics;
	}

	public void setActualSystemStatistics(MetricsEntry actualSystemStatistics,
			long statisticsTimestamp) {
		this.receivingTimestamp = Simulator.getCurrentTime();
		if (skyNetNode.getTreeHandler().isRoot()) {
			this.actualSystemStatistics = actualSystemStatistics;
			this.statisticsTimestamp = Simulator.getCurrentTime();
			log.debug(SkyNetUtilities.getTimeAndNetID(skyNetNode)
					+ "root got new SystemStatistics");
			createNewParametersOfSystemStatistics(this.statisticsTimestamp);
			skyNetNode.getMetricInputStrategy().refreshNeedsUpdateOfAllSubCos();
		} else {
			if (this.statisticsTimestamp < statisticsTimestamp) {
				this.statisticsTimestamp = statisticsTimestamp;
				this.actualSystemStatistics = actualSystemStatistics;
				skyNetNode.getMetricInputStrategy()
						.refreshNeedsUpdateOfAllSubCos();
				log.debug(SkyNetUtilities.getTimeAndNetID(skyNetNode)
						+ "received new SystemStatistics created at "
						+ Simulator.getFormattedTime(this.statisticsTimestamp));
			}
		}
	}

	private void createNewParametersOfSystemStatistics(long manipulatorTS) {
		// TODO this method will start the Data-Mining-process to collect
		// information out of the actual statistics, and writes them later in
		// the class DHTParamterManipulator to manipulate the DHT in accordance
		// to the current statistics (Note: this is only one example of using
		// the collected data)
		paraManipulator = new DHTParamterManipulator();
		this.manipulatorTimestamp = manipulatorTS;
	}

	public DHTParamterManipulator getParaManipulator() {
		return paraManipulator;
	}

	public void setParaManipulator(DHTParamterManipulator paraManipulator,
			long manipulatorTimestamp) {
		if (this.manipulatorTimestamp < manipulatorTimestamp) {
			this.paraManipulator = paraManipulator;
			this.manipulatorTimestamp = manipulatorTimestamp;
			log.debug(SkyNetUtilities.getTimeAndNetID(skyNetNode)
					+ "received new ParameterManipulator-Data");
		}
	}

	public long getStatisticsTimestamp() {
		return statisticsTimestamp;
	}

	public void setStatisticsTimestamp(long statisticsTimestamp) {
		this.statisticsTimestamp = statisticsTimestamp;
	}

	public void setManipulatorTimestamp(long manipulatorTimestamp) {
		this.manipulatorTimestamp = manipulatorTimestamp;
	}

	public long getManipulatorTimestamp() {
		return manipulatorTimestamp;
	}

	public long getReceivingTimestamp() {
		return receivingTimestamp;
	}
}
